<?php

namespace App\Kuafu\Wechat\Controller\Applet;

use App\Kuafu\Customer\Service\CustomerPlatformService;
use App\Kuafu\Order\Service\OrderService;
use App\Kuafu\Wechat\Enum\CodeEnum;
use App\Kuafu\Wechat\Service\WechatCallbackService;
use App\Kuafu\Wechat\Service\WechatSendMessageService;
use App\Kuafu\Wechat\Service\WechatService;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;

class WechatCallbackController extends BaseController
{
    public function getCallback(Request $request)
    {
        WechatCallbackService::valid($request);
    }

    public function postCallback(Request $request)
    {
        WechatCallbackService::responseMsg();
    }

    /**
     * 生成店铺二维码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getQrCode(Request $request)
    {
        $type = (int)($request->type ?? CodeEnum::APPLET_EVENT_TYPE_SHOP);
        $data = ['shopId' => $request->shopId, 'type' => $type];
        $key = CodeEnum::APPLET_EVENT_KEY;
        $qrCode = WechatCallbackService::getQrCode($data, $key);
        $image = WechatCallbackService::getImageByTicket($qrCode['ticket']);
        return $this->success(['type' => $type, 'image' => $image]);
    }

    public function getAccessToken()
    {
        $shopId = $this->shopId;
        $accessToken = WechatCallbackService::getAccessToken($shopId);
        return $this->success($accessToken);
    }

    public function addCardMaterial(Request $request)
    {
        $addCardMaterial = WechatCallbackService::addCardMaterial($request);
        return $this->success(['id' => $addCardMaterial]);
    }

    public function createMenu(Request $request)
    {
        $button = $request->button;
        $menu = WechatCallbackService::createMenu($button);
        return $this->success($menu);
    }

    public function getMenu(Request $request)
    {
        $button = $request->button;
        $menu = WechatCallbackService::getMenu();
        return $this->success($menu);
    }

}

